package search;

import java.util.ArrayList;
import java.util.List;

public class BinarySearch {
    public static void main(String[] args) {
        int[] arr = new int[]{1,7,10,50,600,1000,1000,1000};
//        int index=search(arr,0,arr.length,1000);
        List<Integer> arry=search2(arr,0,arr.length,1000);
        System.out.println("index="+arry);
    }

    private static int search(int[] arr, int lift, int right, int num) {
        if(lift>right){
            return -1;
        }
        int mid=(lift+right)/2;
        if(num>arr[mid]){
            return search(arr,mid+1,right,num);
        }else if(num<arr[mid]){
            return search(arr,lift,mid-1,num);
        }else{
            return mid;
        }
    }
    private static List<Integer> search2(int[] arr, int lift, int right, int num) {
        if(lift>right){
            return new ArrayList<Integer>();
        }
        int mid=(lift+right)/2;
        if(num>arr[mid]){
            return search2(arr,mid+1,right,num);
        }else if(num<arr[mid]){
            return search2(arr,lift,mid-1,num);
        }else{
            List<Integer> list = new ArrayList<Integer>();
            int temp=mid-1;
            while(true){
                if(temp<0||arr[temp]!=num){
                    break;
                }
                System.out.println("---"+temp);
                list.add(temp);
                temp--;
            }
            list.add(mid);
            temp=mid+1;
            while(true){
                if(temp>arr.length-1||arr[temp]!=num){
                    break;
                }
                System.out.println("+++++"+temp);
                list.add(temp);
                temp++;
            }
            return list;
        }
    }
}
